#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Collections.Generic;
using System.Data;
using Flynn.Core.Activities;
using Flynn.Core.ActivityResults;
using MbUnit.Framework;
using Rhino.Mocks;

namespace Flynn.Core.Tests.Activities
{
    [TestFixture]
    public class FilterAndExecuteUpdatesActivityFixture
    {
        private MockRepository _mocks;

        private IFilterUpdatesActivity _filterUpdatesActivity;
        private IExecuteUpdatesActivity _executeUpdatesActivity;
        private IFilterAndExecuteUpdatesActivity _filterAndExecuteUpdatesActivity;

        [SetUp]
        public void SetUp()
        {
            _mocks = new MockRepository();

            _filterUpdatesActivity = _mocks.CreateMock<IFilterUpdatesActivity>();
            _executeUpdatesActivity = _mocks.CreateMock<IExecuteUpdatesActivity>();
            _filterAndExecuteUpdatesActivity = new FilterAndExecuteUpdatesActivity(_filterUpdatesActivity, _executeUpdatesActivity);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FilterUpdatesActivity_Must_Not_Be_Null()
        {
            new FilterAndExecuteUpdatesActivity(null, _executeUpdatesActivity);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ExecuteUpdatesActivity_Must_Not_Be_Null()
        {
            new FilterAndExecuteUpdatesActivity(_filterUpdatesActivity, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Filtering_And_Updating_Updates_Project_Must_Not_Be_Null()
        {
            _filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(null, string.Empty, string.Empty, _mocks.Stub<IDbConnection>());
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Filtering_And_Updating_Updates_Connection_Must_Not_Be_Null()
        {
            _filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(new Project(), string.Empty, string.Empty, null);
        }

        [Test]
        public void Can_Filter_And_Execute_Updates()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                using (_mocks.Ordered())
                {
                    FilterUpdatesResult filterUpdatesResult = FilterUpdatesResult.Success(new List<Update>());
                    Expect.Call(_filterUpdatesActivity.FilterUpdates(project, string.Empty, string.Empty)).Return(filterUpdatesResult);
                    Expect.Call(_executeUpdatesActivity.ExecuteUpdates(filterUpdatesResult.FilteredUpdates, connection)).Return(ExecuteUpdatesResult.Success());
                }
            }

            using (_mocks.Playback())
            {
                FilterAndExecuteUpdatesResult result = _filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, string.Empty, string.Empty, connection);

                Assert.IsFalse(result.Failed);
            }
        }

        [Test]
        public void Filtering_And_Executing_Updates_Fails_If_Filtering_Updates_Fails()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                FilterUpdatesResult filterUpdatesResult = FilterUpdatesResult.Failure("Failure message");
                Expect.Call(_filterUpdatesActivity.FilterUpdates(project, string.Empty, string.Empty)).Return(filterUpdatesResult);
            }

            using (_mocks.Playback())
            {
                FilterAndExecuteUpdatesResult result = _filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, string.Empty, string.Empty, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Filtering_And_Executing_Updates_Fails_If_Filtering_Updates_Throws_Exception()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_filterUpdatesActivity.FilterUpdates(project, string.Empty, string.Empty)).Throw(new Exception("Failure message"));
            }

            using (_mocks.Playback())
            {
                FilterAndExecuteUpdatesResult result = _filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, string.Empty, string.Empty, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Filtering_And_Executing_Updates_Fails_If_Executing_Updates_Fails()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                FilterUpdatesResult filterUpdatesResult = FilterUpdatesResult.Success(new List<Update>());
                Expect.Call(_filterUpdatesActivity.FilterUpdates(project, string.Empty, string.Empty)).Return(filterUpdatesResult);
                Expect.Call(_executeUpdatesActivity.ExecuteUpdates(filterUpdatesResult.FilteredUpdates, connection)).Return(ExecuteUpdatesResult.Failure("Failure message"));
            }

            using (_mocks.Playback())
            {
                FilterAndExecuteUpdatesResult result = _filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, string.Empty, string.Empty, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Filtering_And_Executing_Updates_Fails_If_Executing_Updates_Throws_Exception()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                FilterUpdatesResult filterUpdatesResult = FilterUpdatesResult.Success(new List<Update>());
                Expect.Call(_filterUpdatesActivity.FilterUpdates(project, string.Empty, string.Empty)).Return(filterUpdatesResult);
                Expect.Call(_executeUpdatesActivity.ExecuteUpdates(filterUpdatesResult.FilteredUpdates, connection)).Throw(new Exception("Failure message"));
            }

            using (_mocks.Playback())
            {
                FilterAndExecuteUpdatesResult result = _filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, string.Empty, string.Empty, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }
    }
}